import type {ApiResult, PageParam, PageResult} from '~/types/global'
import type {FsFile, FsFileUploadParam} from '~/types/file/file'
import {request} from '~/utils/request'

enum Api {
  Page = '/fs/file/page',
  Query = '/fs/file/query',
  Common = '/fs/file',
  Batch = '/fs/file/batch',
  Upload = '/fs/file/upload',
}

// 上传文件地址，用于FileUpload组件
export const UploadUrl = Api.Upload

/**
 * 分页查询
 * @return
 */
export function pageFileApi(param: PageParam<FsFile>) {
  return request.post<ApiResult<PageResult<FsFile>>>(Api.Page, { data: param })
}

/**
 * 查询所有
 * @return
 */
export function queryFileApi(param: FsFile = {}) {
  return request.post<ApiResult<FsFile[]>>(Api.Query, { data: param })
}

/**
 * 单体查询
 * @param id
 * @return
 */
export function getFileApi(id: string) {
  return request.get<ApiResult<FsFile>>(`${Api.Common}/${id}`)
}

/**
 * 文件上传
 * @param param
 * @return
 */
export function uploadFileApi(param: FsFileUploadParam) {
  const data = new FormData()
  data.append('bizType', param.bizType || '')
  data.append('file', param.file)
  return request.post<ApiResult<FsFile>>(Api.Upload, {
    data,
    repeatSubmit: false,
  })
}

/**
 * 删除
 * @param id
 * @return
 */
export function deleteFileApi(id: string) {
  return request.delete<ApiResult<boolean>>(`${Api.Common}/${id}`)
}

/**
 * 批量删除
 * @param ids
 * @return
 */
export function batchDeleteFileApi(ids: string[]) {
  return request.delete<ApiResult<boolean>>(Api.Batch, { data: ids })
}
